package ynu.edu.order.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import ynu.edu.common.result.Result;
import ynu.edu.order.dto.CreateOrderDTO;
import ynu.edu.order.service.OrderService;
import ynu.edu.order.vo.OrderVO;

import java.util.List;

/**
 * 订单控制器
 */
@RestController
@RequestMapping("/api/orders")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    /**
     * 创建订单
     * @param createOrderDTO 创建订单DTO
     * @return 订单信息
     */
    @PostMapping
    public Result<OrderVO> createOrder(@RequestBody CreateOrderDTO createOrderDTO) {
        OrderVO orderVO = orderService.createOrder(createOrderDTO);
        return Result.success(orderVO);
    }

    /**
     * 根据订单ID查询订单
     * @param id 订单ID
     * @return 订单信息
     */
    @GetMapping("/{id}")
    public Result<OrderVO> getOrderById(@PathVariable Long id) {
        OrderVO orderVO = orderService.getOrderById(id);
        return Result.success(orderVO);
    }

    /**
     * 根据订单号查询订单
     * @param orderNo 订单号
     * @return 订单信息
     */
    @GetMapping("/no/{orderNo}")
    public Result<OrderVO> getOrderByOrderNo(@PathVariable String orderNo) {
        OrderVO orderVO = orderService.getOrderByOrderNo(orderNo);
        return Result.success(orderVO);
    }

    /**
     * 根据用户ID查询订单列表
     * @param userId 用户ID
     * @param page 页码
     * @param size 每页大小
     * @return 订单分页列表
     */
    @GetMapping("/user/{userId}")
    public Result<Page<OrderVO>> getOrdersByUserId(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<OrderVO> orderVOPage = orderService.getOrdersByUserId(userId, PageRequest.of(page, size));
        return Result.success(orderVOPage);
    }

    /**
     * 根据商家ID查询订单列表
     * 注意：此接口需要使用商家token访问，且只能查询自己的订单
     * @param merchantId 商家ID
     * @param page 页码
     * @param size 每页大小
     * @return 订单分页列表
     */
    @GetMapping("/merchant/{merchantId}")
    public Result<Page<OrderVO>> getOrdersByMerchantId(
            @PathVariable Long merchantId,
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<OrderVO> orderVOPage = orderService.getOrdersByMerchantId(merchantId, PageRequest.of(page, size));
        return Result.success(orderVOPage);
    }

    /**
     * 根据订单状态查询订单列表
     * @param status 订单状态
     * @param page 页码
     * @param size 每页大小
     * @return 订单分页列表
     */
    @GetMapping("/status/{status}")
    public Result<Page<OrderVO>> getOrdersByStatus(
            @PathVariable Integer status,
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<OrderVO> orderVOPage = orderService.getOrdersByStatus(status, PageRequest.of(page, size));
        return Result.success(orderVOPage);
    }

    /**
     * 根据用户ID和订单状态查询订单列表
     * @param userId 用户ID
     * @param status 订单状态
     * @return 订单列表
     */
    @GetMapping("/user/{userId}/status/{status}")
    public Result<List<OrderVO>> getOrdersByUserIdAndStatus(
            @PathVariable Long userId,
            @PathVariable Integer status) {
        List<OrderVO> orderVOList = orderService.getOrdersByUserIdAndStatus(userId, status);
        return Result.success(orderVOList);
    }

    /**
     * 根据商家ID和订单状态查询订单列表
     * 注意：此接口需要使用商家token访问，且只能查询自己的订单
     * @param merchantId 商家ID
     * @param status 订单状态
     * @return 订单列表
     */
    @GetMapping("/merchant/{merchantId}/status/{status}")
    public Result<List<OrderVO>> getOrdersByMerchantIdAndStatus(
            @PathVariable Long merchantId,
            @PathVariable Integer status) {
        List<OrderVO> orderVOList = orderService.getOrdersByMerchantIdAndStatus(merchantId, status);
        return Result.success(orderVOList);
    }

    /**
     * 支付订单
     * @param orderNo 订单号
     * @param payMethod 支付方式
     * @return 订单信息
     */
    @PostMapping("/pay/{orderNo}")
    public Result<OrderVO> payOrder(
            @PathVariable String orderNo,
            @RequestParam Integer payMethod) {
        OrderVO orderVO = orderService.payOrder(orderNo, payMethod);
        return Result.success(orderVO);
    }

    /**
     * 商家接单
     * 注意：此接口需要使用商家token访问
     * @param orderNo 订单号
     * @return 订单信息
     */
    @PostMapping("/accept/{orderNo}")
    public Result<OrderVO> acceptOrder(@PathVariable String orderNo) {
        OrderVO orderVO = orderService.acceptOrder(orderNo);
        return Result.success(orderVO);
    }

    /**
     * 完成订单
     * 注意：此接口需要使用商家token访问
     * @param orderNo 订单号
     * @return 订单信息
     */
    @PostMapping("/complete/{orderNo}")
    public Result<OrderVO> completeOrder(@PathVariable String orderNo) {
        OrderVO orderVO = orderService.completeOrder(orderNo);
        return Result.success(orderVO);
    }

    /**
     * 取消订单
     * @param orderNo 订单号
     * @return 订单信息
     */
    @PostMapping("/cancel/{orderNo}")
    public Result<OrderVO> cancelOrder(@PathVariable String orderNo) {
        OrderVO orderVO = orderService.cancelOrder(orderNo);
        return Result.success(orderVO);
    }

    /**
     * 删除订单
     * @param id 订单ID
     * @return 是否成功
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteOrder(@PathVariable Long id) {
        Boolean result = orderService.deleteOrder(id);
        return Result.success(result);
    }
} 